#include <iostream>
#include <algorithm>
using namespace std;

const long MAXSIZE = 1000000010;
long a[MAXSIZE], b[MAXSIZE], c[MAXSIZE], d[MAXSIZE];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n, m;
    cin >> n;
    while (n--)
    {
        cin >> m;
        for (int i = 1; i <= m; i++)
        {
            cin >> a[i];
            c[i] = a[i];
        }
        sort(c + 1, c + m + 1);
        for (int i = 1; i <= m; i++)
        {
            cin >> b[i];
            d[i] = b[i];
        }
        sort(d + 1, d + m + 1);
        int arrayE = 1, count = 0;
        while (arrayE && count < m)
        {
            if (c[count] != d[count])
                arrayE = 0;
            count++;
        }
        if (!arrayE)
            cout << "NO\n";
        else
        {
            int temp = 0;
            for (int i = 0; i < m; i++)
            {
                for (int j = i; j < m; j++)
                {
                    if (a[i] > b[i])
                    {
                        cout << a[i] << " " << b[i] << "\n";
                        temp = a[i];
                        a[i] = b[i];
                        b[i] = temp;
                    }
                }
            }
        }
    }
    return 0;
}